package com.tmc.web.controller.inthotel;

import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tmc.bean.inthotel.DestinationInfo;
import com.tmc.bean.inthotel.hotel.Hotel;
import com.tmc.vendor.inthotel.tourico.service.HotelService;

@Controller
@RequestMapping("/destination")
public class DestinationController {

	private Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	private HotelService service;
	
	@RequestMapping(value="/searchDestination1", method = RequestMethod.POST)
	@ResponseBody
	public String searchDestination1(String query){
		Hotel h = new Hotel();
	    h.setHotelName(query);
		List<DestinationInfo> infos = service.searchHotel1(h);
	    JSONArray array = new JSONArray();
		for (DestinationInfo info : infos) {
			JSONObject obj = new JSONObject();
			if(info.getNameCN()==null){
				obj.put("Name", info.getName()+"."+info.getFlag());
			}else{
				obj.put("Name", info.getNameCN()+"."+info.getFlag());
			}
			obj.put("Key", info.getKeyValue());
			array.add(obj);
		}
		return array.toJSONString();
	}
	
	@RequestMapping(value="searchDestination", method = RequestMethod.POST)
	@ResponseBody
	public String searchDestination(String query){
		List<Hotel> hotels = service.searchDestination(query);
		JSONArray array = new JSONArray();
		for (Hotel hotel : hotels) {
			JSONObject obj = new JSONObject();
			obj.put("Key", hotel.getId());
			if(hotel.getHotelNameCN()==null){
				if(hotel.getCityNameCN()==null){
					obj.put("Name", hotel.getHotelName()+"."+hotel.getCityName()+"."+hotel.getCountryNameCN());
				}else{
					obj.put("Name", hotel.getHotelName()+"."+hotel.getCityNameCN()+"."+hotel.getCountryNameCN());
				}
			}else{
				if(hotel.getCityNameCN()==null){
					obj.put("Name", hotel.getHotelNameCN()+"."+hotel.getCityName()+"."+hotel.getCountryNameCN());
				}else{
					obj.put("Name", hotel.getHotelNameCN()+"."+hotel.getCityNameCN()+"."+hotel.getCountryNameCN());
				}
			}
			array.add(obj);
		}
		return array.toJSONString();
	}
	
}
